/*
 *  Copyright 2010 johan.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package nu.zoom.catonine.prefs.impl;

import java.io.File;
import java.util.Set;

import nu.zoom.swing.desktop.common.BackendException;
import nu.zoom.swing.desktop.component.filechooser.FileChooser;
import nu.zoom.swing.desktop.component.filechooser.FileChooserFactory;
import nu.zoom.swing.desktop.preferences.InvalidDataTypeException;
import nu.zoom.swing.desktop.preferences.Preferences;

/**
 * An implementation of the preferences interface that uses the zoom.nu desktop
 * preferences.
 * 
 * @author "Johan Maasing" &lt;johan@zoom.nu&gt;
 */
public class PreferencesImpl implements nu.zoom.catonine.prefs.Preferences {
	private static final String MANAGER_FILECHOOSER_PREFS_KEY = "StyleRuleManager.filechooser";
	private static final String FILE_RECENT_PREFERENCESKEY = "svansprogram-recent";
	private static final String DETACHED_WINDOWS = "svansprogram-detached-default";

	private static final String OPENED_FILES_PREFS_KEY = "StyleRuleManager.openfiles";
	private static final String FILE_CONFIGURATION_PREFS_PREFIX = "StyleRuleManager.file.conf.";
	private static final String FILE_GUTTER_PREFS_PREFIX = "StyleRuleManager.file.gutter.";
	private static final String FILE_WRAP_PREFS_PREFIX = "StyleRuleManager.file.wrap.";
	private static final String SCROLLBACK_SIZE = "svansprogram-scrollback-size";

	private final Preferences preferences;
	private final FileChooserFactory fileChooserFactory;

	public PreferencesImpl(final Preferences preferences, final FileChooserFactory fileChooserFactory) {
		this.preferences = preferences;
		this.fileChooserFactory = fileChooserFactory;
	}

	public String getPreferredUUIDForFile(final File file) throws InvalidDataTypeException, BackendException {
		if (file == null) {
			throw new NullPointerException("File may not be null");
		}
		return preferences.getString(PreferencesImpl.FILE_CONFIGURATION_PREFS_PREFIX + file.getName());
	}

	public void setPreferredUUIDForFile(final File file, final String uuid) throws InvalidDataTypeException,
			BackendException {
		preferences.setString(PreferencesImpl.FILE_CONFIGURATION_PREFS_PREFIX + file.getName(), uuid);
	}

	public void rememeberOpenedFiles(final Set<String> filenames) throws InvalidDataTypeException, BackendException {
		preferences.setStrings(OPENED_FILES_PREFS_KEY, (filenames.size() < 1) ? null : filenames);
	}

	public Set<String> getOpenedFiles() throws InvalidDataTypeException, BackendException {
		return preferences.getStrings(OPENED_FILES_PREFS_KEY);
	}
/*
	@Override
	public String getConfigurationFilename() throws InvalidDataTypeException, BackendException {
		return preferences.getString(MANAGER_FILENAME_PREFS_KEY);
	}

	@Override
	public void setConfigurationFilename(String filename) throws InvalidDataTypeException, BackendException {
		preferences.setString(MANAGER_FILENAME_PREFS_KEY, filename);
	}
*/
	@Override
	public FileChooser getConfiguratonFilechooser() throws InvalidDataTypeException, BackendException {
		return fileChooserFactory.getFileChooser(MANAGER_FILECHOOSER_PREFS_KEY);
	}

	@Override
	public FileChooser getFilechooser() throws InvalidDataTypeException, BackendException {
		return fileChooserFactory.getFileChooser(FILE_RECENT_PREFERENCESKEY);
	}

	@Override
	public WindowOpenMode getWindowOpenMode() throws InvalidDataTypeException, BackendException {
		String stringValue = preferences.getString(DETACHED_WINDOWS);
		if (stringValue != null) {
			return WindowOpenMode.valueOf(stringValue);
		} else {
			return WindowOpenMode.Rememeber;
		}
	}

	@Override
	public void setWindowOpenMode(WindowOpenMode value) throws InvalidDataTypeException, BackendException {
		preferences.setString(DETACHED_WINDOWS, value.toString());
	}

	@Override
	public Integer getScrollbackSize() throws InvalidDataTypeException, BackendException {
		return preferences.getInteger(SCROLLBACK_SIZE);
	}

	@Override
	public void setScrollbackSize(Integer size) throws InvalidDataTypeException, BackendException {
		preferences.setInteger(SCROLLBACK_SIZE, size);
	}

	@Override
	public boolean isGutterForFile(File file) throws InvalidDataTypeException, BackendException {
		if (file == null) {
			throw new NullPointerException("File may not be null");
		}
		String value = preferences.getString(PreferencesImpl.FILE_GUTTER_PREFS_PREFIX + file.getName());
		return value != null ? Boolean.parseBoolean(value) : false;
	}

	@Override
	public void setGutterForFile(File file, boolean gutterVisible) throws InvalidDataTypeException, BackendException {
		if (file == null) {
			throw new NullPointerException("File may not be null");
		}
		preferences.setString(PreferencesImpl.FILE_GUTTER_PREFS_PREFIX + file.getName(),
				Boolean.toString(gutterVisible));
	}

	@Override
	public boolean isWrapForFile(File file) throws InvalidDataTypeException, BackendException {
		if (file == null) {
			throw new NullPointerException("File may not be null");
		}
		String value = preferences.getString(PreferencesImpl.FILE_WRAP_PREFS_PREFIX + file.getName());
		return value != null ? Boolean.parseBoolean(value) : false;
	}

	@Override
	public void setWrapForFile(File file, boolean wrap) throws InvalidDataTypeException, BackendException {
		if (file == null) {
			throw new NullPointerException("File may not be null");
		}
		preferences.setString(PreferencesImpl.FILE_WRAP_PREFS_PREFIX + file.getName(), Boolean.toString(wrap));
	}
}
